cmake_minimum_required(VERSION 3.25)

# Find required protobuf package
find_package(protobuf CONFIG REQUIRED)

if(protobuf_FOUND)
    message ("protobuf found")
else()
    message (FATAL_ERROR "Cannot find Protobuf")
endif()

if(protobuf_VERBOSE)
  message(STATUS "Using Protocol Buffers ${protobuf_VERSION}")
endif()

# http://www.cmake.org/Wiki/CMake_FAQ#How_can_I_build_my_MSVC_application_with_a_static_runtime.3F
if(MSVC AND protobuf_MSVC_STATIC_RUNTIME)
  foreach(flag_var
      CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
      CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
    if(${flag_var} MATCHES "/MD")
      string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
    endif(${flag_var} MATCHES "/MD")
  endforeach()
endif()

set(MESSAGE_PROTOS addressbook.proto)

set(library_name proto-message)
set(out_dir "${CMAKE_CURRENT_SOURCE_DIR}/proto_gen")

add_library(${library_name} ${MESSAGE_PROTOS})

protobuf_generate(
    TARGET ${library_name}
    LANGUAGE cpp
    PROTOC_OUT_DIR ${out_dir}
    ${MESSAGE_PROTOS}
)

target_link_libraries(${library_name} protobuf::libprotobuf)
target_include_directories(${library_name} 
    PUBLIC 
        ${PROTOBUF_INCLUDE_DIRS}
        ${out_dir}
    )